const path = require('path');
const Timestamp = new Date().getTime();

module.exports = {
    // webpack 配置
    configureWebpack: {
        // 输出重构 打包编译后的 文件名称  【模块名称.版本号.时间戳】
        output: {
            filename: `[name].${Timestamp}.js`,
            chunkFilename: `[name].${Timestamp}.js`
        },
    },
    publicPath: process.env.VUE_APP_PUBLIC_PATH,
    lintOnSave: process.env.NODE_ENV === 'development',
    productionSourceMap: false,
    devServer: {
        port: process.env.VUE_APP_PORT,
        open: true,
        overlay: {
            warnings: false,
            errors: true
        },
        progress: false
    },
    pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'scss',
            patterns: [
                path.resolve(__dirname, 'src/assets/styles/element/_variables.scss'),
                path.resolve(__dirname, 'src/assets/styles/element/_mixins.scss')
            ]
        }
    },
    chainWebpack(config) {
        //在webpack的名称字段中提供应用程序的标题，以便可以在index.html中对其进行访问以注入正确的标题。
        config.set('name', process.env.VUE_APP_TITLE);

        // https://webpack.js.org/configuration/devtool/#development
        config.when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-eval-source-map'));

        //删除vue-cli-service的进度输出
        config.plugins.delete('progress')
        // 用另一个进度输出插件替换以解决此错误
        // https://github.com/vuejs/vue-cli/issues/4557
        config.plugin('simple-progress-webpack-plugin').use(require.resolve('simple-progress-webpack-plugin'), [{format: 'compact'}])

        config
            .when(process.env.NODE_ENV !== 'development',
                config => {
                    config
                        .optimization.splitChunks({
                        chunks: 'all',
                        cacheGroups: {
                            libs: {
                                name: 'chunk-libs',
                                test: /[\\/]node_modules[\\/]/,
                                priority: 10,
                                chunks: 'initial' // only package third parties that are initially dependent
                            },
                            elementUI: {
                                name: 'chunk-elementUI', // split elementUI into a single package
                                priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                                test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                            },
                            commons: {
                                name: 'chunk-commons',
                                test: path.resolve(__dirname, 'src/components'),
                                minChunks: 3, //  minimum common number
                                priority: 5,
                                reuseExistingChunk: true
                            }
                        }
                    })
                    config.optimization.runtimeChunk('single')
                }
            )
    }
}
